package edu.ujf.sharepas.rcp;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import edu.ujf.sharepas.impl.Category;
import edu.ujf.sharepas.impl.Library;
import edu.ujf.sharepas.impl.ShareFile;
import edu.ujf.sharepas.impl.User;

public class LibraryContentProvider implements ITreeContentProvider {
	
	private static Object[] EMPTY_ARRAY = new Object[0];
	
	protected TreeViewer viewer;
	

	public void dispose() {}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

	}


	/*
	 * @see ITreeContentProvider#getChildren(Object)
	 */
	public Object[] getChildren(Object parentElement) {
		
		if(parentElement instanceof Category){
			Category cat = (Category)parentElement;
			if(cat.hasSubCategory()){
				getChildren(cat);
			}
			List<ShareFile> share = cat.getListShareFile();
			List<IFile> files = new ArrayList<IFile>();
			for(ShareFile f :share ){
				files.add(f.getIFile());
			}
			
			return files.toArray();
		}else if(parentElement instanceof User){
			User user = (User) parentElement;
			return user.getUserLib().getListCategory().toArray();
		}else if(parentElement instanceof List){
			List<User> users = (List<User>) parentElement;
			return users.toArray();
		}
		
		return EMPTY_ARRAY;
	}
	

	/*
	 * @see ITreeContentProvider#getParent(Object)
	 */
	public Object getParent(Object element) {
		return null;
	}

	/*
	 * @see ITreeContentProvider#hasChildren(Object)
	 */
	public boolean hasChildren(Object element) {
		return getChildren(element).length > 0;
	}

	/*
	 * @see IStructuredContentProvider#getElements(Object)
	 */
	public Object[] getElements(Object inputElement) {
		return getChildren(inputElement);
	}



}
